# Copyright (c) 2013, 2015 Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.0)
project(Hunter)

include(ExternalProject) # ExternalProject_Add

# Scheme for download boost and install header-only libraries (no build step)

list(APPEND CMAKE_MODULE_PATH "@HUNTER_SELF@/cmake/modules")

include(hunter_get_boost_libs)
include(hunter_install_boost_config)
include(hunter_report_broken_package)
include(hunter_status_debug)
include(hunter_status_print)
include(hunter_assert_not_empty_string)
include(hunter_user_error)
include(hunter_parse_boost_config_macros)

hunter_status_debug("Scheme: url_sha1_boost")

# Check preconditions
hunter_assert_not_empty_string("@HUNTER_SELF@")
hunter_assert_not_empty_string("@HUNTER_EP_NAME@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_BUILD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@")
hunter_assert_not_empty_string("@HUNTER_Boost_VERSION@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_LICENSE_DIR@")
hunter_assert_not_empty_string("@HUNTER_GLOBAL_SCRIPT_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SCRIPT_DIR@")
hunter_assert_not_empty_string("@HUNTER_TLS_VERIFY@")

# get list of boost components for given version
hunter_get_boost_libs(VERSION "@HUNTER_Boost_VERSION@" LIBS boost_libs)

# MinGW 1.55 is broken
string(REGEX MATCH "1.55.0*" version_1_55 "@HUNTER_Boost_VERSION@")
if(version_1_55 AND MINGW)
  hunter_report_broken_package("MinGW + Boost 1.55.0 (Use >= 1.56.0)")
endif()

foreach(x ${boost_libs})
  list(APPEND boost_list "--without-${x}")
endforeach()

if(CMAKE_HOST_WIN32)
  set(install_cmd "b2")
  if("@MSVC@")
    set(HUNTER_MSVC_VCVARSALL "@HUNTER_MSVC_VCVARSALL@")
    set(HUNTER_MSVC_ARCH_HOST "@HUNTER_MSVC_ARCH_HOST@")
    set(HUNTER_PACKAGE_SOURCE_DIR "@HUNTER_PACKAGE_SOURCE_DIR@")
    configure_file("@HUNTER_PACKAGE_SCRIPT_DIR@/patched_boostrap.bat.in" "@HUNTER_PACKAGE_DOWNLOAD_DIR@/patched_boostrap.bat")
    set(patch_cmd "@CMAKE_COMMAND@" -E copy "@HUNTER_PACKAGE_DOWNLOAD_DIR@/patched_boostrap.bat" "@HUNTER_PACKAGE_SOURCE_DIR@/patched_boostrap.bat")
    set(bootstrap_cmd "patched_boostrap.bat")
  elseif("@MSYS@")
    set(install_cmd "./b2")
    set(patch_cmd "@CMAKE_COMMAND@" -E echo "Dummy patch command")
    set(bootstrap_cmd "./bootstrap.sh")
  else()
    set(patch_cmd "@CMAKE_COMMAND@" -E echo "Dummy patch command")
    set(bootstrap_cmd "bootstrap.bat")
  endif()
else()
  set(install_cmd "./b2")
  set(bootstrap_cmd "./bootstrap.sh")
  set(patch_cmd "@CMAKE_COMMAND@" -E echo "Dummy patch command")
endif()

string(COMPARE EQUAL "@CMAKE_CXX_COMPILER_ID@" "Clang" compiler_is_clang)

if("@MINGW@")
  # Test scenario: MinGW installed in system, Visual Studio - not
  list(APPEND bootstrap_cmd "gcc")
endif()
if(APPLE)
  # Clear Xcode environment
  set(
      bootstrap_cmd
      . "@HUNTER_GLOBAL_SCRIPT_DIR@/clear-all.sh" && ${bootstrap_cmd}
  )
elseif(compiler_is_clang AND NOT CMAKE_CROSSCOMPILING)
  list(APPEND bootstrap_cmd "--with-toolset=clang")
endif()

if("@HUNTER_PACKAGE_LOG_BUILD@")
  set(log_build 1)
else()
  set(log_build 0)
endif()

if("@HUNTER_PACKAGE_LOG_INSTALL@" OR "@HUNTER_SUPPRESS_LIST_OF_FILES@")
  set(log_install 1)
else()
  set(log_install 0)
endif()

hunter_parse_boost_config_macros(BOOST_CONFIG_MACROS "@HUNTER_Boost_CMAKE_ARGS@")
configure_file(
  "@HUNTER_GLOBAL_SCRIPT_DIR@/append-boost-config-macros.cmake.in"
  "@HUNTER_PACKAGE_BUILD_DIR@/append-boost-config-macros.cmake"
  @ONLY
)


set(pass_boost_no_cmake_config )
if (@HUNTER_Boost_VERSION@ VERSION_GREATER_EQUAL 1.72.0 AND NOT USE_CONFIG_FROM_BOOST)
  set(pass_boost_no_cmake_config --no-cmake-config)
endif()

ExternalProject_Add(
    "@HUNTER_EP_NAME@"
    URL
    @HUNTER_PACKAGE_URL@
    URL_HASH
    SHA1=@HUNTER_PACKAGE_SHA1@
    DOWNLOAD_DIR
    "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
    TLS_VERIFY
    "@HUNTER_TLS_VERIFY@"
    SOURCE_DIR
    "@HUNTER_PACKAGE_SOURCE_DIR@"
    INSTALL_DIR
    "@HUNTER_PACKAGE_INSTALL_PREFIX@"
        # not used, just avoid creating Install/<name> empty directory
    UPDATE_COMMAND
    "@CMAKE_COMMAND@" -P
    "@HUNTER_PACKAGE_BUILD_DIR@/append-boost-config-macros.cmake"
    CONFIGURE_COMMAND
    ${patch_cmd}
    COMMAND
    ${bootstrap_cmd}
    BUILD_COMMAND
    ""
    BUILD_IN_SOURCE
    1
    INSTALL_COMMAND
    ${install_cmd}

    --layout=tagged
    -d0
    install
    ${boost_list}
    "--prefix=@HUNTER_PACKAGE_INSTALL_PREFIX@"
    --ignore-site-config # Ignore Gentoo specific optimization "none" in site config that only the patched bjam of Gentoo can understand.
    ${pass_boost_no_cmake_config}
    COMMAND # Copy license files
    "@CMAKE_COMMAND@"
    "-C@HUNTER_ARGS_FILE@" # for 'HUNTER_INSTALL_LICENSE_FILES'
    "-Dsrcdir=@HUNTER_PACKAGE_SOURCE_DIR@"
    "-Ddstdir=@HUNTER_PACKAGE_LICENSE_DIR@"
    -P
    "@HUNTER_SELF@/scripts/try-copy-license.cmake"

    # ineffective on older Boost than 1.72.0 as we resort on --no-cmake-config

    LOG_CONFIGURE ${log_build}
    LOG_BUILD ${log_build}
    LOG_INSTALL ${log_install}
)

# Forward some variables
set(HUNTER_SELF "@HUNTER_SELF@")
set(HUNTER_PACKAGE_INSTALL_PREFIX "@HUNTER_PACKAGE_INSTALL_PREFIX@")
set(HUNTER_Boost_VERSION "@HUNTER_Boost_VERSION@")

if(NOT USE_CONFIG_FROM_BOOST OR HUNTER_Boost_VERSION VERSION_LESS 1.72.0)
  hunter_install_boost_config()
endif()
